import 'package:flutter/material.dart';

class Page3 extends StatefulWidget {
  @override
  _Page3State createState() => _Page3State();
}

class _Page3State extends State<Page3> {
  List<String> results = List();
  List<String> prices1 = List();
  List<String> prices2 = List();

  _doWidgets() {
    List<Widget> listTitles = new List();
    listTitles.add(GestureDetector(
      onTap: () {
        setState(() {
          prices1.clear();
          prices2.clear();
          results.clear();
        });
      },
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: <Widget>[
          Text(
            '总价',
            style: TextStyle(fontSize: 20),
          ),
          Text('重量', style: TextStyle(fontSize: 20)),
          Text('单价', style: TextStyle(fontSize: 20))
        ],
      ),
    ));

    for (var i = 0; i < 5; i++) {
      String result = '结果';
      String price1 = '';
      String price2 = '';
      prices1.add(price1);
      prices2.add(price2);
      results.add(result);
      listTitles.add(buildListItem(context, i));
    }
    return listTitles;
  }

  Widget buildListItem(BuildContext context, int index) {
    var _controller1 =
        TextEditingController.fromValue(TextEditingValue(text: prices1[index]));
    var _controller2 =
        TextEditingController.fromValue(TextEditingValue(text: prices2[index]));
    _controller1.addListener(() {
      setState(() {
        prices1[index] = _controller1.text;
        prices2[index] = _controller2.text;
        if (_controller1.text == '' ||
            _controller2.text == '' ||
            _controller1.text == '0' ||
            _controller2.text == '0') {
          results[index] = '结果';
        } else {
          var a = double.parse(prices1[index]);
          var b = double.parse(prices2[index]);
          var c = a / b;
          results[index] = c.toStringAsFixed(4);
        }
      });
    });
    _controller2.addListener(() {
      setState(() {
        prices1[index] = _controller1.text;
        prices2[index] = _controller2.text;
        if (_controller1.text == '' || _controller2.text == '') {
          results[index] = '结果';
        } else if (_controller1.text == '0' && _controller2.text == '0') {
          results[index] = '结果';
        } else {
          var a = double.parse(prices1[index]);
          var b = double.parse(prices2[index]);
          var c = a / b;
          results[index] = c.toStringAsFixed(4);
        }
      });
    });
    return Column(
      children: <Widget>[
        Container(
          color: Colors.white,
          padding: EdgeInsets.all(15),
          child: Row(
            children: <Widget>[
              Expanded(
                child: TextFormField(
                  controller: _controller1,
                  keyboardType: TextInputType.number,
                  autofocus: false,
                ),
              ),
              Expanded(
                child: TextFormField(
                  controller: _controller2,
                  keyboardType: TextInputType.number,
                  autofocus: false,
                ),
              ),
              Text(results[index], style: TextStyle(fontSize: 20)),
            ],
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: ListView(children: _doWidgets()),
    );
  }
}
